package com.epam.giorgadze.task1.logic;

import com.epam.giorgadze.task1.entity.AirCompany;
import com.epam.giorgadze.task1.entity.plane_type.CargoPlane;
import com.epam.giorgadze.task1.entity.plane_type.PassengerPlane;
import com.epam.giorgadze.task1.entity.plane_type.Plane;
import com.epam.giorgadze.task1.factory.PlaneFactory;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import static com.epam.giorgadze.task1.factory.PlaneFactory.createRandomPlane;

/**
 * User: Luka
 * Date: 08.02.14
 * Time: 22:28
 */
public class AirCompanyLogic {

    private AirCompanyLogic() {
    }

    ;

    public static void sortByTravelingDistance(List<Plane> planes) throws IllegalAccessException, InstantiationException {
        Collections.sort(planes, Plane.TravelingDistanceComparator.TRAVELING_DISTANCE_ORDER);
    }

    public static void sortByNumberOfSeats(List<PassengerPlane> planes) throws IllegalAccessException, InstantiationException {
        Collections.sort(planes, PassengerPlane.NumberOfSeatsComparator.NUMBER_OF_SEATS_ORDER);
    }

    public static void sortByTonnage(List<CargoPlane> planes) throws IllegalAccessException, InstantiationException {
        Collections.sort(planes, CargoPlane.TonnageComparator.TONNAGE_ORDER);
    }

    public static List<Plane> findPlanesByFuelConsumption(double minFuelConsumption, double maxFuelConsumption, List<Plane> planes) {
        List<Plane> list = new ArrayList();
        for (Plane plane : planes) {
            if (plane.getFuelConsumption() >= minFuelConsumption && plane.getFuelConsumption() <= maxFuelConsumption) {
                list.add(plane);
            }
        }
        return list;
    }

    public static int numberOfSeatsSum(List<PassengerPlane> passengerPlanes) {
        int sum = 0;
        for (PassengerPlane passengerPlane : passengerPlanes) {
            sum += passengerPlane.getNumberOfSeats();
        }
        return sum;
    }

    public static double tonnageSum(List<CargoPlane> cargoPlanes) {
        double sum = 0;
        for (CargoPlane cargoPlane : cargoPlanes) {
            sum += cargoPlane.getTonnage();
        }
        return sum;
    }

    public static void addPlane(AirCompany airCompany, PlaneFactory.Planes p, String name, String name1, String email, String address) {
        airCompany.getPlanes().add(createRandomPlane(p, name, name1, email, address));
    }

    public static List<CargoPlane> findCargoPlanes(List<Plane> planes) {
        List<CargoPlane> list = new ArrayList<CargoPlane>();
        for (Plane plane : planes) {
            if (plane instanceof CargoPlane) list.add((CargoPlane) (plane));
        }
        return list;
    }

    public static List<PassengerPlane> findPassangerPlanes(List<Plane> planes) {
        List<PassengerPlane> list = new ArrayList<PassengerPlane>();
        for (Plane plane : planes) {
            if (plane instanceof PassengerPlane) list.add((PassengerPlane) (plane));
        }
        return list;
    }

}


